#!/usr/bin/perl -c

package PI::Configuration::Main;

=head1 NAME

PI::Configuration::Main - PI's main configuration file

=head1 SYNOPSIS

  use PI::Configuration::Main;
  $c = PI::Configuration::Main->instance;

  print $c->data->{default_area};
  $c->data->{area}->{home} = '/usr/local';
  $c->save;

=head1 DESCRIPTION

This class maintains PI's main configuration file. The class is a singleton
registy.

=cut


use strict;

use base 'PIPL::Configuration',
         'PIPL::Class::Singleton';


# Private constructor used by singleton
sub _new {
    my $class = shift;
    my $self = $class->SUPER::new($class->file, $class->default_data);
    bless($self, $class);

    # Load the configuration into data attribute
    $self->load;

    $self;
}


# Instance of singleton class
my $self;


# Singleton constructor
sub instance {
    $self ||= (shift)->_new;
}


# Initializes file name
sub file {
    my $self = shift;
    my $file = File::Spec->catfile($self->configuration_directory, 'config.yml');

    $file;
}

# Initializes data structure
sub default_data {
    my $self = shift;
    my $home = PI::System::Path->home;
    my $data = {
        'area' => {
            'perl' => PI::System::Config->installarchlib,
            'home_perl' => File::Spec->catdir($home, 'perl/lib'),
            'site' => PI::System::Config->installsitearch,
            'home' => File::Spec->catdir($home, 'site_perl'),
            'vendor' => PI::System::Config->installvendorarch,
        },
        'default_area' => 'home_perl',
        'repo' => {
            'ActiveState' => {
                'url' => sprintf('http://ppm.activestate.com/PPMPackages/%d.%d-%s/package.xml.gz',
                    PI::System::Config->api_revision,
                    PI::System::Config->api_version,
                    PI::System::Config->osname),
                'name' => 'ActiveState Package Repository',
                'enabled' => 'yes',
            }
        },
    };
    if (defined $File::Spec::Unix::VERSION or
        defined $File::Spec::Cygwin::VERSION)
    {
        $data->{area}->{usr_local} = '/usr/local/lib/site_perl';
        $data->{area}->{usr_local_perl} = '/usr/local/perl/lib';
    }

    $data;
}


1;


=head1 ATTRIBUTES

=head2 data

Contains the data of configuration file.

=head1 CONSTRUCTORS

=head2 instance

Creates the singleton instance.  Using it guarantees that only one instance
of configuration class is used.

=head1 METHODS

=head2 load

=head1 AUTHORS

Piotr Roszatycki E<lt>dexter@debian.orgE<gt>

=head1 COPYRIGHT

Copyright 2006 by Piotr Roszatycki E<lt>dexter@debian.orgE<gt>.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>
